package com.ciban.smarthome.module.ui;

import java.util.ArrayList;

import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.ciban.smarthome.R;
import com.ciban.smarthome.module.ui.base.BaseActivity;
import com.ciban.smarthome.module.db.dao.HostDao;
import com.ciban.smarthome.module.entity.Host;
import com.ciban.smarthome.module.ui.adapter.HostShowAdapter;

public class HostActivity extends BaseActivity implements OnClickListener {
    private static final int INTENT_ADD_HOST = 100;

    private ImageView iv_back, iv_add;
    private ListView lv_show;
    private Button bt_del, bt_cancle;
    private LinearLayout ll_bottom;

    private ArrayList<Host> list;
    private HostShowAdapter adapter;
    private boolean isDeling = false; // 是否正在删除模式下

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_host_details);

        initViews();

        initListView();
    }


    private void initListView() {
        HostDao dao = new HostDao(this);
        if (list != null) {
            list.clear();
            list.addAll(dao.getMainEngList());
        } else
            list = dao.getMainEngList();
        if (adapter == null) {
            adapter = new HostShowAdapter(this, list);
            lv_show.setAdapter(adapter);
        } else {
            adapter.notifyDataSetChanged();
        }
        dao = null;
    }

    private void refreshEditView() {
        if (isDeling) {
            ll_bottom.setVisibility(View.VISIBLE);
        } else {
            ll_bottom.setVisibility(View.GONE);
        }
        adapter.setEdit(isDeling);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.title_back:
                finish();
                break;
            case R.id.title_add:
                showOprWin(iv_add);
                break;
            case R.id.host_bt_cancle:
                isDeling = false;
                refreshEditView();
                for (Host info : list)
                    info.setSelcted(false);
                break;
            case R.id.host_bt_del:
                ArrayList<Host> tmpList = new ArrayList<Host>();
                HostDao dao = new HostDao(this);
                for (Host info : list) {
                    if (!info.isSelcted())
                        continue;
                    else {
                        dao.delMainEngine(info.getId());
                        tmpList.add(info);
                    }
                }
                list.removeAll(tmpList);
                tmpList.clear();
                tmpList = null;
                adapter.notifyDataSetChanged();
                isDeling = false;
                refreshEditView();
                break;
        }
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        switch (requestCode) {
            case INTENT_ADD_HOST:
                if (resultCode == RESULT_OK)
                    initListView();
                break;

            default:
                break;
        }
    }

    private void initViews() {
        ((TextView) findViewById(R.id.title)).setText(R.string.my_host);
        iv_back = (ImageView) findViewById(R.id.title_back);
        iv_add = (ImageView) findViewById(R.id.title_add);
        lv_show = (ListView) findViewById(R.id.host_lv_show);

        ll_bottom = (LinearLayout) findViewById(R.id.host_lv_bottom);
        bt_cancle = (Button) findViewById(R.id.host_bt_cancle);
        bt_del = (Button) findViewById(R.id.host_bt_del);

        setListeners();
    }

    private void setListeners() {
        iv_back.setOnClickListener(this);
        iv_add.setOnClickListener(this);
        bt_cancle.setOnClickListener(this);
        bt_del.setOnClickListener(this);

        lv_show.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1,
                                    int position, long arg3) {
                Host info = list.get(position);
                if (isDeling) {
                    info.setSelcted(!info.isSelcted());
                    adapter.notifyDataSetChanged();
                } else {
                    Intent intent = new Intent(HostActivity.this,
                            HostModifyActivity.class);
                    intent.putExtra("host", info);
                    startActivityForResult(intent, INTENT_ADD_HOST);
                }
            }
        });
    }

    @Override
    protected void oprEdit() {
        popOprwin.dismiss();
        if (isDeling)
            return;
        isDeling = true;
        refreshEditView();
    }

    @Override
    protected void oprAdd() {
        popOprwin.dismiss();
        Intent intent = new Intent(HostActivity.this, HostModifyActivity.class);
        startActivityForResult(intent, INTENT_ADD_HOST);
    }

}
